<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>房间种类管理</el-breadcrumb-item>
      <el-breadcrumb-item>房间种类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form
      :inline="true"
      :model="form"
      label-width="80px"
      @submit.native.prevent
    >
      <el-form-item label="查询房间种类">
        <el-input
          prefix-icon="el-icon-search"
          @keyup.native.enter="queryType"
          v-model="form.typeId"
          placeholder="请输入房间种类ID"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryType" type="primary" round>搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="type" style="width: 100%">
      <el-table-column label="房间种类ID" prop="typeId"></el-table-column>
      <el-table-column label="房间种类名称" prop="typeName"></el-table-column>
      <el-table-column label="房间种类价格" prop="typePrice"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="delType(scope.row.typeId)"
            circle
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-view"
            circle
          ></el-button>
          <el-button
            @click="modifyType(scope.row.typeId)"
            type="success"
            size="small"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-cold-drink"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="pageChange"
      background
      :page-size="pagesize"
      :current-page="page"
      layout="->,prev, pager, next"
      :total="sum"
    >
    </el-pagination>
    <el-dialog title="修改房间种类" :visible.sync="dialogFormVisible">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="房间种类名称" prop="typeName">
          <el-input v-model="form.typeName"></el-input>
        </el-form-item>
        <el-form-item label="房间种类价格" prop="typePrice">
          <el-input v-model="form.typePrice"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="confirmModify">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        typeId: "",
        typeName: "",
        typePrice: "",
      },
      type: [],
      pagesize: 6,
      page: 1,
      sum: 0,
      dialogFormVisible: false,
      rules: {
        typeName: [
          { required: true, message: "房间种类名称不能为空", trigger: "blur" },
        ],
        typePrice: [
          { required: true, message: "价格不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    //加载房间种类列表  分页查询
    let params = { page: this.page, pagesize: this.pagesize };
    this.$http.typeApi
      .pageList(params)
      .then((res) => {
        //console.log(res);
        this.type = res.data.data[0];
        this.sum = res.data.data[1][0].sum;
      })
      .catch((err) => console.log(err));
  },
  methods: {
    // 通过id查房间种类
    queryType() {
      if (this.form.typeId) {
        this.$http.typeApi
          .queryById(this.form.typeId)
          .then((res) => {
            console.log(res);
            this.type = res.data.data;
          })
          .catch((err) => console.log(err));
      } else {
        this.loadType();
      }
    },
    // 加载房间种类列表
    loadType() {
      //加载房间种类列表  分页查询
      let params = { page: this.page, pagesize: this.pagesize };
      this.$http.typeApi
        .pageList(params)
        .then((res) => {
          //console.log(res);
          this.type = res.data.data[0];
          this.sum = res.data.data[1][0].sum;
        })
        .catch((err) => console.log(err));
    },
    //删除房间种类
    delType(bid) {
      this.$http.typeApi
        .delete({ typeId: bid })
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "删除房间种类成功",
            });
            this.loadType();
          }
        })
        .catch((err) => console.log(err));
    },
    // 页码改变时，列表也变化
    pageChange(page) {
      //console.log(page);
      this.page = page;
      let params = { page, pagesize: this.pagesize };
      this.$http.typeApi
        .pageList(params)
        .then((res) => {
          //console.log(res);
          this.type = res.data.data[0];
          this.sum = res.data.data[1][0].sum;
        })
        .catch((err) => console.log(err));
    },
    // 修改房间种类
    modifyType(id) {
      this.dialogFormVisible = true;
      this.$store.commit("setTypeId", id);
    },
    // 确认修改
    confirmModify() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            typeId: this.$store.state.typeId,
            typeName: this.form.typeName,
            typePrice: this.form.typePrice,
          };
          this.$http.typeApi
            .modify(params)
            .then((res) => {
              //console.log(res);
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改房间种类成功！",
                });
                this.$refs["form"].resetFields();
                this.dialogFormVisible = false;
                this.loadType();
              }
            })
            .catch((err) => console.log(err));
        } else {
          this.$message({
            type: "error",
            message: "表单填写错误！",
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
<style lang="scss">
.el-dialog__title {
  color: #409eff;
  font-size: 27px;
  font-weight: bold;
}
.el-form-item__label {
  color: orangered;
}
</style>
